package makovo.utils;

import java.io.IOException;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SSLGMail extends AsyncMail {
	
	public SSLGMail() throws IOException {
		super();
	}

	private String mMessage;
	private String mFrom;
	private String mSubject;
	private InternetAddress[] mAddresses;

	@Override
	public String getHost() {
		return getProperties().getProperty("mail.smtp.host");
	}

	@Override
	public Mail setHost(String host) {
		getProperties().put("mail.smtp.host", host);

		return this;
	}

	@Override
	public int getFactoryPort() {
		return (int) getProperties().get("mail.smtp.socketFactory.port");
	}

	@Override
	public Mail setFactoryPort(int port) {
		getProperties().put("mail.smtp.socketFactory.port", String.valueOf(port));

		return this;
	}

	@Override
	public String getFactoryClass() {

		return (String) getProperties().get("mail.smtp.socketFactory.class");
	}

	@Override
	public Mail setFactoryClass(String className) {
		getProperties().put("mail.smtp.socketFactory.class", className);

		return this;
	}

	@Override
	public boolean getAuth() {
		return (boolean) getProperties().get("mail.smtp.auth");

	}

	@Override
	public Mail setAuth(boolean auth) {
		getProperties().put("mail.smtp.auth", String.valueOf(auth));

		return this;
	}

	@Override
	public int getPort() {
		return (int) getProperties().get("mail.smtp.port");
	}

	@Override
	public Mail setPort(int port) {
		getProperties().put("mail.smtp.port", String.valueOf(port));

		return this;
	}

	@Override
	public String getFrom() {
		return mFrom;
	}

	@Override
	public Mail setFrom(String sourceMail) {
		this.mFrom = sourceMail;

		return this;
	}

	@Override
	public Mail setMessage(String message) {
		this.mMessage = message;

		return this;
	}

	@Override
	public Mail setSubject(String subject) {
		this.mSubject = subject;

		return this;
	}

	@Override
	public String getSourceMail() {
		return getProperties().getProperty("mail.source");
	}

	@Override
	public Mail setRecipients(InternetAddress[] addresses) {
		this.mAddresses = addresses;

		return this;
	}

	@Override
	public void sendMail() {
		Session session = Session.getInstance(getProperties(), new Authenticator() {

			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication((String) getProperties().get("mail.source"), (String) getProperties().get(
						"mail.pass"));
			}

		});

		Message message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress((String) getProperties().get("mail.source")));

			message.setRecipients(Message.RecipientType.TO, this.mAddresses);

			message.setSubject(this.mSubject);
			message.setText(this.mMessage);

			Transport.send(message);
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}

	}

}
